<template>
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm" :model="valiFormData" :label-position="'top'" :formItem="formData"></my-forms>
			<u-button type="primary" shape="circle" text="保存任务" @click="submit"></u-button>
		</view>
	</view>
</template>
<script setup>
	import {
		computed,
		reactive,
		ref
	} from 'vue'
	import {
		listRiskPointManagement,
		addControlInfo
	} from '@/api/riskcontrol.js'
	import date from '@/utils/date.js'

	// 校验表单数据
	let formData = computed(() => {
		return [{
			itemType: 'select',
			list: [{
				dictLabel: '日常管控',
				dictValue: '1'
			}, {
				dictLabel: '领导带班检查',
				dictValue: '2'
			}],
			label: '管控类型',
			required: true,
			prop: 'riskControlType'
		}, {
			itemType: 'date',
			type: 'date',
			label: '管控时间',
			required: true,
			prop: 'riskControlDate'
		}, {
			itemType: 'select',
			type: 'GETuseList',
			label: '管控人',
			required: true,
			prop: 'userIds',
			multiple: true
		}, {
			label: '排查名称',
			required: true,
			prop: 'riskControlName',
			show: valiFormData.riskControlType == 1
		}, {
			itemType: 'select',
			label: '管控地点',
			list: checkList.value,
			required: true,
			prop: 'riskLocationIds',
			multiple: true
		}, {
			itemType: 'select',
			label: '风险等级',
			required: true,
			type: 'GETriskLevel',
			prop: 'riskLevels',
			multiple: true
		}, ].filter(i => i)
	})
	const valiForm = ref(null)
	const valiFormData = reactive({
		riskControlDate: date(new Date(), 'YYYY-MM-DD'),
	})
	// 保存提交
	const submit = () => {
		valiForm.value.$refs.refMyForm.validate().then(async (res) => {
			// valiFormData.userIds = [valiFormData.userIds]
			// valiFormData.riskLocationIds = [valiFormData.riskLocationIds]
			// valiFormData.riskLevels = [valiFormData.riskLevels]
			const {
				code,
				msg
			} = await addControlInfo(valiFormData)
			if (code == 200) {
				uni.showToast({
					icon: 'success',
					title: `新增成功`
				})
				// valiFormData.userIds = []
				// valiFormData.riskLocationIds = []
				// valiFormData.riskLevels = []
				uni.navigateBack({
					delta: 1 // 返回的页面数，如果是1则返回上一页
				})
			} else {
				uni.showToast({
					title: msg
				})
			}
		}).catch(err => {
			console.log('err', err);
		})
	}
	// 获取检查地点列表
	let checkList = ref([])
	const getCheckPonit = async () => {
		const {
			data
		} = await listRiskPointManagement()
		checkList.value = data.map((item) => ({
			dictLabel: item.riskLocationName,
			dictValue: item.id
		}))
	}
	getCheckPonit()
</script>
<style lang="scss" scoped>
	.card {
		padding: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}
</style>